package com.etcbase.dcs;

import java.io.InputStream;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
import android.util.Log;

import com.etcbase.dcs.controller.lookup.LookupService;
import com.etcbase.dcs.controller.map.AddressResolver;
import com.etcbase.dcs.controller.map.MapNoteDao;
import com.etcbase.dcs.model.lookup.LookupServiceData;
import com.etcbase.dcs.model.lookup.Lookupable;

/**
 * This is the GLOBAL APPLICATION OBJECT ! (1) all common and shared data will
 * accessed from here (2) program initialization will take place here, before
 * login screen is shown
 */
public class DcsApp extends Application {
	public static final String TAG = DcsApp.class.getSimpleName();

	// for ease of development use this global switch - if this turned on:
	// - city and district data won't be read and kept short to keep the
	// initialization time short
	// - login screen won't be shown, instead the acitivity on which we are
	// focued is shown directly!
	public static final boolean DEVELOPMENT_MODE = true;
	public static final boolean DEVELOPMENT_SKIP_LOGIN = true;
	public static final boolean DEVELOPMENT_MOCK_DISTRICTS = false;
	public static final boolean DEVELOPMENT_TEST_LOOKUP_SERVICE = true;

	public int animationDurationtime = 1000;
	public boolean animationsEnabled = true;

	private MapNoteDao mapNoteViewDataController;
	private AddressResolver addressResolver;
	private String userName;

	private LookupServiceData lookupServiceData;

	/*
	 * Called when application starts even before the main-activity.
	 */
	@Override
	public void onCreate() {
		super.onCreate();

		// initialize the database connector
		mapNoteViewDataController = new MapNoteDao(getApplicationContext());

		// initialize address name resolver
		addressResolver = new AddressResolver();
		/* TODO neyi select ediyoruz burada */
		String select = getResources().getString(R.string.SPINNER_SELECT);
		InputStream xmlDistricts = getResources().openRawResource(
				R.raw.districts);
		addressResolver.initialize(xmlDistricts, select);

		// TODO: where should we start the following lookup service?
		lookupServiceData = new LookupServiceData();
		Intent service = new Intent(getApplicationContext(),
				LookupService.class);
		LookupServiceFinished resultReceiver = new LookupServiceFinished(
				new Handler());
		service.putExtra("resultReceiver", resultReceiver);
		startService(service);
	}

	class LookupServiceFinished extends ResultReceiver {

		public LookupServiceFinished(Handler handler) {
			super(handler);
		}

		@Override
		protected void onReceiveResult(int resultCode, Bundle resultData) {
			super.onReceiveResult(resultCode, resultData);

			LookupServiceData ls = lookupServiceData;
			Log.d("************", "size = " + ls.serviceFileTypeList.size());
			Log.d("************", "size = " + ls.tenantList.size());
			Log.d("************", "size = " + ls.contactTypeList.size());
			Log.d("************", "size = " + ls.lookupRepositoryMap.size());
			Log.d("************", "size = " + ls.lookupRepositoryListMap.size());

			for (Lookupable item : ls.serviceFileTypeList) {
				Log.d("************", "-------------------------------");
				Log.d("************", "id = " + item.getId());
				Log.d("************", "name = " + item.getName());
				Log.d("************", "enum = " + item.getEnumValue());
			}

			for (Lookupable item : ls.tenantList) {
				Log.d("************", "-------------------------------");
				Log.d("************", "id = " + item.getId());
				Log.d("************", "name = " + item.getName());
				Log.d("************", "enum = " + item.getEnumValue());
			}

			for (Lookupable item : ls.contactTypeList) {
				Log.d("************", "-------------------------------");
				Log.d("************", "id = " + item.getId());
				Log.d("************", "name = " + item.getName());
				Log.d("************", "enum = " + item.getEnumValue());
			}

		}

	}

	public LookupServiceData getLookupServiceData() {
		// the intent-service might be updating this..
		synchronized (lookupServiceData) {
			return lookupServiceData;
		}
	}

	public AddressResolver getAddressResolver() {
		return addressResolver;
	}

	public MapNoteDao getMapNoteViewDataBaseConnector() {
		return mapNoteViewDataController;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserName() {
		return (DEVELOPMENT_SKIP_LOGIN ? "test" : userName);
	}

	public boolean isNetworkAvailable() {
		ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetworkInfo = connectivityManager
				.getActiveNetworkInfo();
		return activeNetworkInfo != null;
	}
}
